#!/usr/bin/env python
from __future__ import absolute_import, division, print_function
# This script generates PX4 parameters from YAML files

import argparse
import yaml

TYPE_DICT = {
    int: 6,
    float: 9
}


def main():
    parser = argparse.ArgumentParser(description='YAML to PX4 parameters',
                                     formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('yaml', type=str, help='YAML file')
    parser.add_argument('-s', '--system-id', type=int, default=1, help='System ID')
    parser.add_argument('-c', '--component-id', type=int, default=1, help='Component ID')
    # parser.add_argument('-o', '--output', type=str, default=None, help='Output file name')

    args = parser.parse_args()

    yaml_dict = yaml.load(open(args.yaml, 'r'))

    print('# Autogenerated parameters\n')

    for key, value in yaml_dict.iteritems():
        template = '{system}\t{component}\t{name}\t{value}\t{type}'
        row = template.format(system=args.system_id,
                              component=args.component_id,
                              name=key,
                              value=value,
                              type=TYPE_DICT[type(value)])
        print(row)


if __name__ == '__main__':
    main()
